// app.js
const { request, post, get, put, del, uploadFile } = require('./utils/http')
App({
  data:{
    globalSocket:"",//websocket
    globalSudit:"",//audit
  },
  onLaunch() {
    this.initAudit()
    if(wx.getStorageSync('token')){
      this.initWebSocket()
    }
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  /* WebSocket初始化配置 */
  initWebSocket: function (){
    const { miniProgram } = wx.getAccountInfoSync()
    const { envVersion } = miniProgram
    let socketUrl = ""
    switch (envVersion) {
      // 开发版
      case "develop":
        socketUrl = "ws://192.168.1.177:48080/"
        // socketUrl = "wss://llcs.sxndkj.net/"
        break;
        // 体验版
      case "trial":
        socketUrl = "wss://llcs.sxndkj.net/"
        break;
        // 正式版
      case "release":
        socketUrl = "wss://llcs.sxndkj.net/"
        break;
      default:
        socketUrl = "ws://192.168.1.177:80/"
        break;
    }
    const that = this;
    const url = socketUrl+'infra/ws?token='+wx.getStorageSync('token');
    let llcsSocket = wx.connectSocket({
      url: url,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log('WebSocket连接成功', res);
      },
      fail: function (err) {
        console.error('WebSocket连接失败', err);
      }
    });
    that.data.globalSocket = llcsSocket
    that.data.globalSocket.onMessage(function (message) {
      console.log("小程序入口接受的消息",message)
      that.globalSudit.src = `${that.globalData.serverImg}audio/default.mp3`;//获取音频地址
      if(wx.getStorageSync('userInfo')?.isMsgReminder){
        that.globalSudit.play();//播放
      }
    })
   
    /* 监听webSocket事件 */
    // wx.onSocketOpen(function(res) {
    //   setInterval(()=>{
    //     globalSocket.send({
    //       data:"ping"
    //     })
    //   },3000)
    // })
  },
  /* 音频初始化配置 */
  initAudit:function(){
    const that = this;
    that.globalSudit = wx.createInnerAudioContext();
  },
  toast: (title, duration = 1500) => {
    wx.showToast({
      title,
      icon: 'none',
      duration
    })
  },
  toastTo: (title, callback, duration = 1500, icon = 'success') => {
    wx.showToast({
      title, duration, icon,
      success: () => {
        setTimeout(() => {
          callback&&callback()
        }, duration)
      }
    })
  },
  loading: (title, mask,callback) => {
    wx.showLoading({
      title,
      mask:mask || false,
      success: () => {
        callback&&callback()
      }
    })
  },
  hideLoading:(duration,callback)=>{
    setTimeout(()=>{
      wx.hideLoading({
        success: () => {
          callback&&callback()
        }
      })
    },duration || 2000)
  },
  get,
  post,
  put,
  del,
  request,
  uploadFile,
  globalData: {
    userInfo: null,
    serverAddress:""
  }
})
const globalApp = getApp()
globalApp.globalUtil = require('./utils/util.js') //挂载工具函数
globalApp.globalApis = require('./utils/api.js')  //挂载api请求地址
